<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <label for="file">上传图片格式验证</label>
    <input type="file" id="file">
    <script>
        //jpg png 
        window.onload=function(){
            //获取标签
            var file=document.getElementById('file');
            //监听图片选择的变化
            file.onchange=function(){
            //获取上传的图片路径
            var path=this.value;
            //获取‘。’在路径字符串中占的位置
            var loc=path.lastIndexOf('.');
            //文件路径的后缀名
            var suffix=path.substr(loc);
            //转小写
            var lower_suffix=suffix.toLowerCase();
            //判断
            if(lower_suffix==='.jpg'||lower_suffix==='.png'||lower_suffix==='.gif'||lower_suffix==='.jpeg'){
               alert('上传图片格式正确') ;
            }else{
                alert('上传图片格式不正确') ;
            }
            }
        }
    </script>
</body>
</html>